package com.xjc.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xjc.business.entity.domain.*;
import com.xjc.business.entity.vo.schadmin.HomeViewDataVo;
import com.xjc.business.service.*;
import com.xjc.system.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 徐锦程
 * 时间:2025-02-18 18:20
 */
@Service
public class SchoolHomeServiceImpl implements SchoolHomeService {

    @Autowired
    @Lazy
    SchoolCollegeRelationsService schoolCollegeRelationsService;

    @Autowired
    @Lazy
    CollegesService collegesService;

    @Autowired
    @Lazy
    AllStudentRegsiterRelationsService allStudentRegsiterRelationsService;

    @Autowired
    @Lazy
    SchoolBuildingRelationsService schoolBuildingRelationsService;

    @Autowired
    @Lazy
    BuildingsService buildingsService;

    @Autowired
    @Lazy
    AllStudentStayRelationsService allStudentStayRelationsService;

    @Autowired
    @Lazy
    StudentsService studentsService;

    @Autowired
    @Lazy
    SchoolCollageAdminRelationsService schoolCollageAdminRelationsService;

    @Autowired
    @Lazy
    SchoolBuildingAdminRelationsService schoolBuildingAdminRelationsService;

    @Autowired
    @Lazy
    SchoolDoorAdminRelationsService schoolDoorAdminRelationsService;


    /**
     * 获取首页可视化数据
     * @param schoolId 学校id
     * @return Result<HomeViewDataVo>
     */
    @Override
    public Result<HomeViewDataVo> getHomeViewData(Integer schoolId) {

        HomeViewDataVo homeViewDataVo = new HomeViewDataVo();

        // 学院人数对比
        LambdaQueryWrapper<SchoolCollegeRelations> schoolCollegeRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        schoolCollegeRelationsLambdaQueryWrapper.eq(SchoolCollegeRelations::getSchId,schoolId);
        List<Integer> colIdList = schoolCollegeRelationsService.list().stream().map(SchoolCollegeRelations::getColId).collect(Collectors.toList());

        List<String> xAxisDataList = new ArrayList<>();

        List<Integer> seriesDataList = new ArrayList<>();

        colIdList.forEach(colId -> {
            String name = collegesService.getById(colId).getName();
            xAxisDataList.add(name);
            LambdaQueryWrapper<AllStudentRegsiterRelations> allStudentRegsiterRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
            allStudentRegsiterRelationsLambdaQueryWrapper.eq(AllStudentRegsiterRelations::getColId,colId);
            long count = allStudentRegsiterRelationsService.count(allStudentRegsiterRelationsLambdaQueryWrapper);
            seriesDataList.add((int) count);
        });

        homeViewDataVo.setXAxisDataList(xAxisDataList);
        homeViewDataVo.setSeriesDataList(seriesDataList);

        // 宿舍人数对比
        LambdaQueryWrapper<SchoolBuildingRelations> schoolBuildingRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        schoolBuildingRelationsLambdaQueryWrapper.eq(SchoolBuildingRelations::getSchId,schoolId);
        List<Integer> buildIdList = schoolBuildingRelationsService.list().stream().map(SchoolBuildingRelations::getBid).collect(Collectors.toList());

        List<String> xAxisDataList2 = new ArrayList<>();

        List<Integer> seriesDataList2 = new ArrayList<>();

        buildIdList.forEach(buildId -> {
            Buildings buildings = buildingsService.getById(buildId);
            xAxisDataList2.add(buildings.getBno()+"号楼");

            LambdaQueryWrapper<AllStudentStayRelations> allStudentStayRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
            allStudentStayRelationsLambdaQueryWrapper.eq(AllStudentStayRelations::getBuildId,buildId);
            long count = allStudentStayRelationsService.count(allStudentStayRelationsLambdaQueryWrapper);
            seriesDataList2.add((int) count);
        });

        homeViewDataVo.setXAxisDataList2(xAxisDataList2);
        homeViewDataVo.setSeriesDataList2(seriesDataList2);

        // 员工类型比例
        List<HomeViewDataVo.PieData> pieDataList = new ArrayList<>();

        LambdaQueryWrapper<SchoolCollageAdminRelations> schoolCollageAdminRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        schoolCollageAdminRelationsLambdaQueryWrapper.eq(SchoolCollageAdminRelations::getSchId,schoolId);
        long collageAdminCount = schoolCollageAdminRelationsService.count(schoolCollageAdminRelationsLambdaQueryWrapper);
        HomeViewDataVo.PieData pieData1 = new HomeViewDataVo.PieData();
        pieData1.setName("学院管理员");
        pieData1.setValue((int) collageAdminCount);
        pieDataList.add(pieData1);

        LambdaQueryWrapper<SchoolBuildingAdminRelations> schoolBuildingAdminRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        schoolBuildingAdminRelationsLambdaQueryWrapper.eq(SchoolBuildingAdminRelations::getSchId,schoolId);
        long buildingAdminCount = schoolBuildingAdminRelationsService.count(schoolBuildingAdminRelationsLambdaQueryWrapper);
        HomeViewDataVo.PieData pieData2 = new HomeViewDataVo.PieData();
        pieData2.setName("宿舍管理员");
        pieData2.setValue((int) buildingAdminCount);
        pieDataList.add(pieData2);

        LambdaQueryWrapper<SchoolDoorAdminRelations> schoolDoorAdminRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        schoolDoorAdminRelationsLambdaQueryWrapper.eq(SchoolDoorAdminRelations::getSchId,schoolId);
        long doorAdminCount = schoolDoorAdminRelationsService.count(schoolDoorAdminRelationsLambdaQueryWrapper);
        HomeViewDataVo.PieData pieData3 = new HomeViewDataVo.PieData();
        pieData3.setName("门卫管理员");
        pieData3.setValue((int) doorAdminCount);
        pieDataList.add(pieData3);

        homeViewDataVo.setPieDataList(pieDataList);

        // 学生性别比例
        List<HomeViewDataVo.PieData> pieDataList2 = new ArrayList<>();

        LambdaQueryWrapper<AllStudentRegsiterRelations> allStudentRegsiterRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        allStudentRegsiterRelationsLambdaQueryWrapper.eq(AllStudentRegsiterRelations::getSchId,schoolId);
        List<Integer> stuIdList = allStudentRegsiterRelationsService.list(allStudentRegsiterRelationsLambdaQueryWrapper).stream().map(AllStudentRegsiterRelations::getStuId).collect(Collectors.toList());

        if (stuIdList.isEmpty()){
            HomeViewDataVo.PieData pieData4 = new HomeViewDataVo.PieData();
            pieData4.setName("男生");
            pieData4.setValue(0);
            pieDataList2.add(pieData4);

            HomeViewDataVo.PieData pieData5 = new HomeViewDataVo.PieData();
            pieData5.setName("女生");
            pieData5.setValue(0);
            pieDataList2.add(pieData5);

            homeViewDataVo.setPieDataList2(pieDataList2);

            return Result.okWithData(homeViewDataVo);
        }
        LambdaQueryWrapper<Students> studentsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        studentsLambdaQueryWrapper.in(Students::getId,stuIdList);
        Map<Integer, List<Students>> sexMap = studentsService.list(studentsLambdaQueryWrapper).stream().collect(Collectors.groupingBy(Students::getSex));
        List<Students> manList = sexMap.get(1);
        List<Students> womanList = sexMap.get(0);

        HomeViewDataVo.PieData pieData4 = new HomeViewDataVo.PieData();
        pieData4.setName("男生");
        pieData4.setValue(manList!=null?manList.size():0);
        pieDataList2.add(pieData4);

        HomeViewDataVo.PieData pieData5 = new HomeViewDataVo.PieData();
        pieData5.setName("女生");
        pieData5.setValue(womanList!=null?womanList.size():0);
        pieDataList2.add(pieData5);

        homeViewDataVo.setPieDataList2(pieDataList2);

        return Result.okWithData(homeViewDataVo);
    }
}